package com.zis.common;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 跨域请求过滤器
 * @author wangshen
 *
 */
public class CrossDomainFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {
		
		String jsonCallback = ((HttpServletRequest) req).getParameter("jsonpCallback");
		if (jsonCallback != null) {
			HttpServletResponse response = (HttpServletResponse) res;
			OutputStream os = response.getOutputStream();
			os.write((jsonCallback + "(").getBytes());
			chain.doFilter(req, res);
			os.write(")".getBytes());
		} else {
			chain.doFilter(req, res);
		}
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
